<?php
namespace app\home\model;
use \think\Db;
use think\Model;
use think\Session;

class AddressModel extends Model{

    public function addressList(){
        //获取当前用户的所有收货地址
        $data['user_id']= Session::get('USER.id');
        $data['is_del']=0;
        $rows = Db::table('user_address')->where($data)->select();
        return   $rows;
    }
    //设为默认地址
    public function setDefault($id){
        //先将当前用户的所有收货地址默认设为0再设置单条
        $chickid['user_id']= Session::get('USER.id');
        $data['user_id']= Session::get('USER.id');
        $data['id']=$id  ;
        $update['default_status']=0  ;
        $update2['default_status']=1  ;
        $res = Db::table('user_address')->where($chickid)->update($update);
        $rows = Db::table('user_address')->where($data)->update($update2);
        return   $rows;
    }
    //删除默认地址
    public function deleteAddress($id){
        $data['user_id']= Session::get('USER.id');
        $data['id']=$id;
        $update['is_del']=1;
        $rows = Db::table('user_address')->where($data)->update($update);
        return   $rows;
    }
    //三级联动 省
    public function getProvince(){
        $data['parentId']=0;
        $rows = Db::table('areas')->where($data)->select();
        return   $rows;
    }
    //可用于超链接和ajax获取城市
    public function getCityId($areaId){
        $data['parentId']=$areaId;
        $rows = Db::table('areas')->where($data)->select();
        return   $rows;
    }
    public function doAddAddress($data){
        $rows = Db::table('user_address')->insert($data);
        return   $rows;
    }
    public function updateAddress($id){
        $data['id']=$id;
        $rows = Db::table('user_address')->where($data)->find();
        return   $rows;
    }
    public function doUpdateAddress($data,$where){
        $rows = Db::table('user_address')->where($where)->update($data);
        return   $rows;
    }
    //查看是否有默认地址
    public function defaultAddress(){
        $data['user_id']= Session::get('USER.id');
        $data['is_del']=0;
        $data['default_status']=1;
        $rows = Db::table('user_address')->where($data)->find();
        return   $rows;
    }

}